package com.abe.libquick.utils.quick;

import android.widget.RadioButton;


import com.abe.libquick.utils.Annotation.Parent;
import com.abe.libquick.utils.Annotation.SortableField;

import java.util.List;

@SuppressWarnings({"rawtypes", "unchecked"})
public class RadioButtonUtils {

    /**
     * 获取选中数据
     */
    public static String getRButton(RadioButton... temp) {
        String str = "";
        for (RadioButton btn : temp) {
            if (btn.isChecked()) {
                str = btn.getText().toString();
            }
        }
        return str;
    }

    public static String getRButton(List<RadioButton> temp) {
        String str = "";
        for (RadioButton btn : temp) {
            if (btn.isChecked()) {
                str = btn.getText().toString();
            }
        }
        return str;
    }

    public static String getRButtonTag(RadioButton... temp) {
        String str = "";
        for (RadioButton btn : temp) {
            if (btn.isChecked()) {
                str = btn.getText().toString();
            }
        }
        return str;
    }

    /**
     * 根据数据，初始化UI
     */
    public static void setRButton(String str, RadioButton... temp) {
        for (RadioButton btn : temp) {
            if (str != null && str.equals(btn.getText().toString())) {
                btn.setChecked(true);
            }
        }
    }

    public static void setRButtonTag(String str, RadioButton... temp) {
        for (RadioButton btn : temp) {
            if (str != null && str.equals(btn.getTag().toString())) {
                btn.setChecked(true);
            }
        }
    }

    public static void setRButton(List<RadioButton> temp, String str) {
        for (RadioButton btn : temp) {
            if (str != null && str.equals(btn.getText().toString())) {
                btn.setChecked(true);
            }
        }
    }

    public static void setRButton(RadioButton[] temp, String str) {
        for (RadioButton btn : temp) {
            if (str != null && str.equals(btn.getText().toString())) {
                btn.setChecked(true);
            }
        }
    }

    public static <T> void setRButtonPrint(Class<T> clazz, Object obj, RadioButton[] temp, String str) {
        if (str != null && !str.equals("")) {
            Parent a = new Parent<>(clazz);
            List<SortableField> list = a.init();
            for (RadioButton btn : temp) {
                String btnName = btn.toString().substring(
                        btn.toString().indexOf("/") + 1,
                        btn.toString().indexOf("}"));
                @SuppressWarnings("unused")
                String btnType = btn.getClass().getSimpleName();
                for (SortableField l : list) {
                    if (l.getName().equals(btnName)
                            && str.equals(btn.getText().toString())) {
                        try {
                            l.getField().set(obj, "On");
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}